package com.wanmait.myhouse.dao;

import com.wanmait.myhouse.util.DBHelper;
import com.wanmait.myhouse.vo.Contact;
import com.wanmait.myhouse.vo.Pager;
import com.wanmait.myhouse.vo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ContactDAO {
    public void addContact(Contact contact){//添加联系
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into t_contact (userId,mediatorId,content,dispose,time) values(?,?,?,?,?)";
        try {
            queryRunner.update(sql,contact.getUserId(),contact.getMediatorId(),contact.getContent(),contact.getDispose(),contact.getTime());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public List<Contact> FindById(Integer id , Pager pager){//用id查
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_contact where userId = ? order by dispose asc limit ?,?";
        int start = (pager.getPageNO() - 1) * pager.getPageSize();
        try {
            List<Contact> contactList = queryRunner.query(sql,new BeanListHandler<>(Contact.class),id,start,pager.getPageSize());
            return contactList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    public Contact FindBy(Contact contact){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_contact where id=?";
        try {
            Contact contact1 = queryRunner.query(sql, new BeanHandler<>(Contact.class), contact.getId());
            return contact1;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    public void add(Contact contact){//添加意见
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "update t_contact set reply=? ,dispose=? where id=?";
        try {
            queryRunner.update(sql,contact.getReply(),contact.getDispose(),contact.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public int getTotal(Integer id){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select count(*) as total from t_contact where mediatorId=?";
        try {
            Object o = queryRunner.query(sql,new ScalarHandler<>(),id);
            int total = Integer.parseInt(o.toString());
            return (int) total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public int getTotal(Contact contact){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select count(*) as total from t_contact where userId=?";
        try {
            Object o = queryRunner.query(sql,new ScalarHandler<>(),contact.getUserId());
            int total = Integer.parseInt(o.toString());
            return (int) total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public List<Contact> select(Pager pager , Integer id){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_contact where mediatorId = ? order by dispose asc limit ?,?";
        int start = (pager.getPageNO()-1)*pager.getPageSize();
        try {
            List<Contact> contactList = queryRunner.query(sql, new BeanListHandler<>(Contact.class),id,start,pager.getPageSize());
            return contactList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

}
